import datetime

import pymysql
from datetime import datetime

def get_connection_cursor():
    connection = pymysql.Connect(host="127.0.0.1", port=3306, database="cdlg",
                                 user="root", password="000000", charset="utf8mb4")
    cursor = connection.cursor()
    return connection, cursor

# 增删改
def execute_(sql, cursor):
    return cursor.execute(sql)

# 查询
def query_(sql, cursor):
    cursor.execute(sql)
    return cursor.fetchall()

# 提交事务
def commit_(connection):
    if connection:
        connection.commit()

# 回滚事务
def rollback_(connection):
    if connection:
        connection.rollback()

# 关闭连接和游标
def close_(connection, cursor):
    if connection:
        connection.close()
    if cursor:
        cursor.close()

def get_all_users_test():
    select_sql = "select * from user"

    connection, cursor = None, None
    try:
        connection, cursor = get_connection_cursor()
        results = query_(select_sql, cursor)
        return results

    except BaseException as e:
        print(e)

    finally:
        close_(connection, cursor)

# if __name__ == '__main__':
#     print(get_all_users_test())